const p2s = require(`${__dirname}/../src/index.js`);
const yaml = require('yaml');
const fs = require('fs');
const path = require('path')

const sourceDir = path.join(__dirname, '../source')
// console.log(sourceDir)
const dir = fs.readdirSync(sourceDir);
// console.log(dir)
// const files = dir.filter(element => fs.statSync(path.join(dir, element)).isFile);

dir.forEach(function(filename){
// console.log(filename)
generateYaml(filename)
})

// for (const file of files) {
//   console.log(file)
//   // generateYaml(file)
//   // console.log(path.basename(file,'.json'))
// }

function generateYaml(file){
  const postmanJson = yaml.parse(fs.readFileSync(__dirname + '/../source/'+ file,'utf8'));
  const openapiJson = p2s(postmanJson, {
    info: {
      version: '1.0.0'
    }
  });
  
  const output = yaml.stringify(openapiJson);
  // console.log(output)
  const filename = path.basename(file, '.json')
  const dest = __dirname+'/../output/' + filename + '.yaml'
  console.log(dest)
  fs.writeFile(dest,output, ()=>null)
  
}

